package com.kolon.business.mAccessMan.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jdom.Document;
import org.jdom.Element;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kolon.framework.constant.Constant;
import com.kolon.framework.interfaces.SimpleController;
import com.kolon.framework.util.ComUtil;
import com.kolon.framework.util.MsgUtil;

@Controller("com.kolon.business.mAccessMan.controller.MAccessManApplySearchController")
public class MAccessManApplySearchController implements SimpleController {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value="/business/mAccessMan/mAccessManApplySearch.do")
	public ModelAndView controller(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// 도메인
		String url = MsgUtil.getMessage("xml.url");
		// 세션 아이디 및 암호 값을 불러옴
		HttpSession httpSession = request.getSession();
		Map userMap = (Map)httpSession.getAttribute(Constant.SESSION_USER_INFO);
		String id = (String)userMap.get("userId");
		String pw = (String)userMap.get("userPw");
		// 파라미터
		String startdate = ComUtil.nullChk(request.getParameter("startdate"));
		String enddate = ComUtil.nullChk(request.getParameter("enddate"));
		String searchType = ComUtil.nullChk(request.getParameter("searchType"));
		String searchVal = ComUtil.nullChk(request.getParameter("searchVal"));
		String searchdata = "";
		String companyname = "";
		if("searchdata".equals(searchType)) {
			searchdata = ComUtil.nullChk(ComUtil.getEncode(request.getParameter("searchVal"),"EUC-KR"));
		}else if("companyname".equals(searchType)) {
			companyname = ComUtil.nullChk(ComUtil.getEncode(request.getParameter("searchVal"),"EUC-KR"));
		}
		ModelAndView modelAndView = new ModelAndView();
		StringBuffer buffer = new StringBuffer();
		buffer.append(url);
		buffer.append("/access_man.aspx?command=").append("select");
		buffer.append("&startdate=").append(startdate);
		buffer.append("&enddate=").append(enddate);			
		buffer.append("&vStatus=").append("all");
		buffer.append("&searchdata=").append(searchdata);
		buffer.append("&companyname=").append(companyname);
		buffer.append("&recent=");
		buffer.append("&userid=").append(id);
		// XML DOCUMENT 및 인증 처리
		Document doc = ComUtil.getDocument(buffer.toString(), id, pw);
		Element element = doc.getRootElement();
		List xmlList = element.getChildren("visitor");
		Iterator itXml = xmlList.iterator();
		List mapList = new ArrayList(); 
		// XML 데이터 
		while (itXml.hasNext()) {
			Element el = (Element)itXml.next();
			Map map = new HashMap();
			map.put("APPLY_SEQ", el.getChild("APPLY_SEQ").getText());
			map.put("VISITOR_STATUS", el.getChild("VISITOR_STATUS").getText());
			map.put("VISITOR_NAME", el.getChild("VISITOR_NAME").getText());
			map.put("VISITOR_COMP_NAME", el.getChild("VISITOR_COMP_NAME").getText());
			map.put("RESERVATION_DATE", el.getChild("RESERVATION_DATE").getText());
			map.put("RESERVATION_START_TIME", el.getChild("RESERVATION_START_TIME").getText());
			map.put("RESERVATION_END_TIME", el.getChild("RESERVATION_END_TIME").getText());
			mapList.add(map);
		}
		modelAndView.addObject("mapList", mapList);
		modelAndView.addObject("startdate", startdate);
		modelAndView.addObject("enddate", enddate);
		modelAndView.addObject("searchType", searchType);
		modelAndView.addObject("searchVal", searchVal);
		// 리턴
		return modelAndView;
	}
}